package charazay.tool.managers.XML;


import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.LocatorImpl;

import charazay.tool.cobjects.Player;
import charazay.tool.cobjects.Team;
import charazay.tool.managers.XML.interfaces.PlayerInterface;
import charazay.tool.managers.site.TeamSiteManager;

public class PlayerFileContentHandler implements ContentHandler, PlayerInterface {
	
	Player newplayer = null;
	StringBuffer text;
	private Team team = null;
	private String username = null;
	private String password = null;
	
	private Locator locator;
	
    public PlayerFileContentHandler(String username, String password) {
    	super();
    	locator = new LocatorImpl();
        // On definit le locator par defaut.
    	this.username = username;
    	this.password = password;
    }
	
    public Player getPlayer() {
    	return newplayer;
    }
    
	@Override
	public void characters(char[] ch, int start, int end) throws SAXException {
		// TODO Auto-generated method stub
		String contenu = new String(ch, start, end);
    	if (text != null) 
    		text.append(contenu);
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void endElement(String nameSpaceURI, String localName, String rawName)
			throws SAXException {
		// TODO Auto-generated method stub
		if ( localName.equals(NAME))
			newplayer.setFirstName(text.toString());
		if ( localName.equals(SURNAME))
			newplayer.setLastName(text.toString());
		if ( localName.equals(AGE))
			newplayer.setAge(Integer.parseInt(text.toString()));
		if ( localName.equals(HEIGHT))
			newplayer.setHeight(Integer.parseInt(text.toString()));
		if ( localName.equals(WEIGHT))
			newplayer.setWeight(Float.parseFloat(text.toString()));
		if ( localName.equals(SI))
			newplayer.setSi(Integer.parseInt(text.toString()));
		if ( localName.equals(SALARY))
			newplayer.setSalary(Integer.parseInt(text.toString()));

		
		if ( localName.equals(DEFENCE))
			newplayer.setDefence(Integer.parseInt(text.toString()));
		if ( localName.equals(FREETHROW))
			newplayer.setFreeThrow(Integer.parseInt(text.toString()));
		if ( localName.equals(TWOPOINT))
			newplayer.setTwoPts(Integer.parseInt(text.toString()));
		if ( localName.equals(THREEPOINT))
			newplayer.setThreePts(Integer.parseInt(text.toString()));
		if ( localName.equals(DRIBLING))
			newplayer.setDribbling(Integer.parseInt(text.toString()));
		if ( localName.equals(SPEED))
			newplayer.setSpeed(Integer.parseInt(text.toString()));
		if ( localName.equals(FOOTWORK))
			newplayer.setFootwork(Integer.parseInt(text.toString()));
		if ( localName.equals(REBOUND))
			newplayer.setRebond(Integer.parseInt(text.toString()));
		if ( localName.equals(PASSING))
			newplayer.setPassing(Integer.parseInt(text.toString()));
		if ( localName.equals(XP))
			newplayer.setExperience(Integer.parseInt(text.toString()));


	}

	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDocumentLocator(Locator arg0) {
		// TODO Auto-generated method stub
		locator =  arg0;

	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub

	}

	@Override
	public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributs) throws SAXException {
		// TODO Auto-generated method stub
		
		text = new StringBuffer();
		if ( localName.equals(PLAYER) ) {
			newplayer = new Player();
			newplayer.setCharazay_id(Integer.parseInt(attributs.getValue(ID)));
			newplayer.setCountry(Integer.parseInt(attributs.getValue(COUNTRY)));
			if (team == null) {
				TeamSiteManager teamManager = new TeamSiteManager(Integer.parseInt(attributs.getValue(TEAM)));
				team = teamManager.request(username, password);
			}
			newplayer.setTeam(team); //get Team !
		}

	}

	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
		// TODO Auto-generated method stub

	}

}
